{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "/usr/local/python3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "/usr/local/python3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "/usr/local/python3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "/usr/local/python3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "/usr/local/python3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "/usr/local/python3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "from keras.preprocessing import image     #导入numpy、matplotlib、keras、cv2\n",
    "import numpy as np\n",
    "from keras.models import load_model\n",
    "import cv2\n",
    "from matplotlib import pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /usr/local/python3/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "WARNING:tensorflow:From /usr/local/python3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "模型概括：\n",
      "Model: \"sequential_1\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d_1 (Conv2D)            (None, 28, 28, 16)        160       \n",
      "_________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooling2 (None, 14, 14, 16)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_2 (Conv2D)            (None, 14, 14, 32)        4640      \n",
      "_________________________________________________________________\n",
      "max_pooling2d_2 (MaxPooling2 (None, 7, 7, 32)          0         \n",
      "_________________________________________________________________\n",
      "flatten_1 (Flatten)          (None, 1568)              0         \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (None, 1568)              0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 128)               200832    \n",
      "_________________________________________________________________\n",
      "dropout_2 (Dropout)          (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 10)                1290      \n",
      "=================================================================\n",
      "Total params: 206,922\n",
      "Trainable params: 206,922\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "网络层数： 9\n",
      "每一层的名称： [<keras.layers.convolutional.Conv2D object at 0x7fe7e02bb7f0>, <keras.layers.pooling.MaxPooling2D object at 0x7fe7a0823cf8>, <keras.layers.convolutional.Conv2D object at 0x7fe7a0581128>, <keras.layers.pooling.MaxPooling2D object at 0x7fe79fd27f28>, <keras.layers.core.Flatten object at 0x7fe79fd27eb8>, <keras.layers.core.Dropout object at 0x7fe79fcfb940>, <keras.layers.core.Dense object at 0x7fe7a0806eb8>, <keras.layers.core.Dropout object at 0x7fe79fd1a9e8>, <keras.layers.core.Dense object at 0x7fe79fcc8940>]\n"
     ]
    }
   ],
   "source": [
    " #加载模型\n",
    "model = load_model('./model1.h5')    \n",
    "# 1、模型概括打印\n",
    "print(\"模型概括：\")\n",
    "model.summary()\n",
    "#获取网络层数\n",
    "print(\"网络层数：\",len(model.layers))\n",
    "#获取每一层的名称\n",
    "print(\"每一层的名称：\",model.layers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<keras.layers.convolutional.Conv2D object at 0x7fe7e02bb7f0>\n",
      "=========================================================================\n",
      "该层权重的的维度： 2\n",
      "=========================================================================\n",
      "卷积权重的类型： <class 'numpy.ndarray'>\n",
      "卷积核原本的形状： (3, 3, 1, 16)\n",
      "卷积核转置后的形状： (16, 1, 3, 3)\n",
      "=========================================================================\n",
      "偏置的类型： <class 'numpy.ndarray'>\n",
      "偏置的形状： (16,)\n"
     ]
    }
   ],
   "source": [
    "#获取第零层卷积的权重和偏置\n",
    "print((model.layers[0]))       #打印第0层网络的名称\n",
    "layer0 = model.get_layer(index=0) #获取改层\n",
    "weights0 = layer0.get_weights()   #获取该层的参数W和b\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"该层权重的的维度：\",len(weights0))              #该层权重的的维度：卷积和偏置两项\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"卷积权重的类型：\",type(weights0[0]))           #打印该层的权重的类型\n",
    "#weights0[0]=weights0[0].astype(np.int16)            #将权重数据类型转为int16\n",
    "\n",
    "\n",
    "print(\"卷积核原本的形状：\",weights0[0].shape)             #打印该层的权重的形状：3行3列16通道\n",
    "#转置3x3x1x16=====>16x1x3x3\n",
    "weights0[0]=weights0[0].transpose(3,2,0,1)\n",
    "print(\"卷积核转置后的形状：\",weights0[0].shape)             #打印该层的权重的形状：3行3列16通道\n",
    "#print(weights0[0])             #打印该层的权重\n",
    "#保存卷积权重到二进制bin文件\n",
    "weights0[0].tofile(\"./weight_bin//c1w.bin\")\n",
    "#scio.savemat(\"./c1w.mat\", {'A':weights0[0]})        #c1权重保存到mat文件中\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"偏置的类型：\",type(weights0[1]))           #打印该层的权重的类型\n",
    "print(\"偏置的形状：\",weights0[1].shape)             #打印该层的权重的形状：3行3列16通道\n",
    "#转置3x3x1x16=====>16x1x3x3\n",
    "#weights0[0]=weights0[0].transpose(3,2,0,1)\n",
    "#print(weights0[1])             #打印该层的权重\n",
    "weights0[1].tofile(\"./weight_bin/c1b.bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=========================================================================\n",
      "该层权重的的维度： 2\n",
      "=========================================================================\n",
      "卷积权重的类型： <class 'numpy.ndarray'>\n",
      "卷积核原本的形状： (3, 3, 16, 32)\n",
      "卷积核转置后的形状： (32, 16, 3, 3)\n",
      "[[[[ 0.0955558   0.03922452 -0.25097427]\n",
      "   [ 0.16857785 -0.02725656 -0.19132093]\n",
      "   [-0.08175548 -0.10033172 -0.10125921]]\n",
      "\n",
      "  [[-0.04053079 -0.15918678 -0.0583311 ]\n",
      "   [ 0.15658604 -0.15012737  0.11678957]\n",
      "   [-0.05627985  0.08495566 -0.03348925]]\n",
      "\n",
      "  [[-0.02489801 -0.1430895   0.01578913]\n",
      "   [-0.05740668  0.17014503  0.12443769]\n",
      "   [-0.2960375   0.00900051 -0.02006085]]\n",
      "\n",
      "  ...\n",
      "\n",
      "  [[ 0.12181476 -0.06771509  0.04779996]\n",
      "   [ 0.11980715 -0.09162667 -0.06758997]\n",
      "   [ 0.06297724 -0.03517004 -0.11981961]]\n",
      "\n",
      "  [[ 0.0995675  -0.04470449  0.06225052]\n",
      "   [ 0.01354606 -0.06548665  0.00283639]\n",
      "   [-0.11552183  0.07060124  0.14280926]]\n",
      "\n",
      "  [[-0.05744582 -0.09331529 -0.13445722]\n",
      "   [ 0.01491377  0.04618784  0.02726056]\n",
      "   [-0.06708395 -0.02387498  0.07066359]]]\n",
      "\n",
      "\n",
      " [[[ 0.22696668 -0.1819491  -0.16990462]\n",
      "   [-0.15594646 -0.14613439 -0.03920652]\n",
      "   [-0.20097618  0.04570065  0.05844005]]\n",
      "\n",
      "  [[-0.06427912 -0.21440889 -0.12137978]\n",
      "   [-0.08775865 -0.02736151  0.01478237]\n",
      "   [-0.06337792  0.01433417 -0.0914579 ]]\n",
      "\n",
      "  [[-0.11288723 -0.18062535 -0.04444933]\n",
      "   [-0.00262237  0.1112766   0.05882576]\n",
      "   [ 0.06163361  0.06907459 -0.04262268]]\n",
      "\n",
      "  ...\n",
      "\n",
      "  [[ 0.1428195  -0.03340613  0.06406144]\n",
      "   [-0.11480092  0.07513218 -0.04198392]\n",
      "   [-0.04770089  0.0324305  -0.15857896]]\n",
      "\n",
      "  [[ 0.08630023 -0.18081671  0.01564561]\n",
      "   [-0.04858375 -0.08454546 -0.11072223]\n",
      "   [-0.12930466 -0.00612494 -0.11376762]]\n",
      "\n",
      "  [[-0.03673221 -0.10361026 -0.03102699]\n",
      "   [ 0.06277362 -0.03077513 -0.07522719]\n",
      "   [-0.03662864  0.05499087 -0.04932363]]]\n",
      "\n",
      "\n",
      " [[[-0.21872485 -0.04600206 -0.14681925]\n",
      "   [ 0.01665934 -0.02006933  0.03747949]\n",
      "   [-0.17593639 -0.17213732  0.05774368]]\n",
      "\n",
      "  [[ 0.09787745  0.10909182 -0.09165466]\n",
      "   [ 0.16484576  0.03061994 -0.13649926]\n",
      "   [-0.05635973 -0.002563   -0.16347776]]\n",
      "\n",
      "  [[ 0.06752263  0.14683254 -0.01003337]\n",
      "   [ 0.02388988 -0.01064829 -0.18452373]\n",
      "   [ 0.01037424 -0.14303687  0.02130029]]\n",
      "\n",
      "  ...\n",
      "\n",
      "  [[-0.12177341 -0.08957919 -0.16445845]\n",
      "   [ 0.00520639 -0.00602881 -0.06175489]\n",
      "   [-0.05027001 -0.08693469 -0.07113945]]\n",
      "\n",
      "  [[-0.02910148  0.02873401  0.00695461]\n",
      "   [-0.13651685  0.02747775 -0.04680306]\n",
      "   [-0.05423657 -0.09667549 -0.06652523]]\n",
      "\n",
      "  [[ 0.0311785  -0.02156552 -0.07727773]\n",
      "   [ 0.1695022  -0.1279485  -0.2453099 ]\n",
      "   [-0.06154018 -0.15760812 -0.10621265]]]\n",
      "\n",
      "\n",
      " ...\n",
      "\n",
      "\n",
      " [[[ 0.11492304  0.08673267  0.15369599]\n",
      "   [-0.01333063  0.04694709 -0.03870806]\n",
      "   [-0.10907562 -0.06849442 -0.31090716]]\n",
      "\n",
      "  [[-0.10798996  0.01238123 -0.19137774]\n",
      "   [ 0.2176468  -0.08031992 -0.13961822]\n",
      "   [ 0.16935898 -0.24578127  0.0980451 ]]\n",
      "\n",
      "  [[ 0.05910715 -0.0878487   0.05905874]\n",
      "   [ 0.20518392  0.01249476 -0.0188356 ]\n",
      "   [ 0.10596403 -0.00944016  0.10929343]]\n",
      "\n",
      "  ...\n",
      "\n",
      "  [[-0.07838719  0.07891905  0.04706323]\n",
      "   [-0.04638236 -0.11148796 -0.11688848]\n",
      "   [-0.1092254  -0.18160552  0.07852828]]\n",
      "\n",
      "  [[ 0.14624691 -0.0396365  -0.03911755]\n",
      "   [-0.05237772 -0.01441112 -0.09694204]\n",
      "   [-0.01471843 -0.26216203 -0.04304836]]\n",
      "\n",
      "  [[-0.01173772  0.08996396 -0.13015623]\n",
      "   [ 0.18777029 -0.09588093 -0.10576962]\n",
      "   [-0.02950554 -0.20902899  0.12420236]]]\n",
      "\n",
      "\n",
      " [[[ 0.04590278 -0.17860317  0.07137967]\n",
      "   [ 0.11221285  0.02754417  0.1649909 ]\n",
      "   [ 0.13192077  0.03744936  0.21013734]]\n",
      "\n",
      "  [[ 0.01160652 -0.05767563  0.02043308]\n",
      "   [-0.06728684  0.04393332  0.15020432]\n",
      "   [-0.05554047  0.03982605  0.04815052]]\n",
      "\n",
      "  [[-0.02357912  0.1299404   0.02791881]\n",
      "   [-0.00641432  0.01906344  0.17811503]\n",
      "   [ 0.08427557  0.08187588  0.0295646 ]]\n",
      "\n",
      "  ...\n",
      "\n",
      "  [[-0.0918024   0.09235491  0.11111336]\n",
      "   [-0.09318294  0.09984319  0.03431117]\n",
      "   [-0.03362895 -0.11303517  0.08155603]]\n",
      "\n",
      "  [[-0.14578183 -0.02882885  0.02866718]\n",
      "   [-0.11142359  0.03753383  0.14893681]\n",
      "   [-0.00074115 -0.05917517  0.01807706]]\n",
      "\n",
      "  [[-0.17798659 -0.05959046 -0.02164852]\n",
      "   [-0.08222741  0.09652362 -0.00293337]\n",
      "   [ 0.05938501 -0.06835072 -0.020749  ]]]\n",
      "\n",
      "\n",
      " [[[ 0.04179184  0.04210502 -0.04969551]\n",
      "   [ 0.17156662  0.07597131  0.00634526]\n",
      "   [ 0.21742888 -0.06360203 -0.15638229]]\n",
      "\n",
      "  [[-0.2654291  -0.07560079  0.11508243]\n",
      "   [-0.00591572  0.01821787 -0.05184484]\n",
      "   [ 0.08886689 -0.17988265  0.15651368]]\n",
      "\n",
      "  [[-0.3529334   0.18662412  0.03572281]\n",
      "   [ 0.045844    0.04694109  0.04382665]\n",
      "   [ 0.06063257  0.05691431  0.01751063]]\n",
      "\n",
      "  ...\n",
      "\n",
      "  [[-0.16509196 -0.13736776 -0.06241374]\n",
      "   [-0.035858   -0.07968777 -0.0676527 ]\n",
      "   [ 0.02124985 -0.05784285 -0.01186398]]\n",
      "\n",
      "  [[-0.02915951 -0.19493788  0.06419022]\n",
      "   [ 0.09188127  0.01845524 -0.00555864]\n",
      "   [ 0.17320712 -0.0070974  -0.00921163]]\n",
      "\n",
      "  [[-0.05958049 -0.1320519   0.01292856]\n",
      "   [ 0.01105548 -0.03466342 -0.12745084]\n",
      "   [-0.0253722  -0.00596388  0.11007153]]]]\n",
      "=========================================================================\n",
      "偏置的类型： <class 'numpy.ndarray'>\n",
      "偏置原本的形状： (32,)\n",
      "[-0.02272152 -0.00626088 -0.04959327  0.00986751  0.00141975 -0.12516804\n",
      " -0.0922258  -0.0444097  -0.04616328 -0.07257318 -0.04877454 -0.0233861\n",
      " -0.15989418 -0.00274181 -0.00959518  0.01387289 -0.0105739  -0.01821423\n",
      " -0.07709751 -0.0169714   0.0065144  -0.0057805  -0.03332941 -0.07496818\n",
      " -0.00788418 -0.00096073 -0.0157764  -0.00654608 -0.0430148  -0.03144505\n",
      " -0.00370333 -0.04282686]\n"
     ]
    }
   ],
   "source": [
    "#获取第二层卷积的权重和偏置\n",
    "layer2 = model.get_layer(index=2) #获取改层\n",
    "weights2 = layer2.get_weights()   #获取该层的参数W和b\n",
    "print('=========================================================================')\n",
    "print(\"该层权重的的维度：\",len(weights2))              #该层权重的的维度：卷积和偏置两项\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"卷积权重的类型：\",type(weights2[0]))           #打印该层的权重的类型\n",
    "print(\"卷积核原本的形状：\",weights2[0].shape)             #打印该层的权重的形状：3行3列16通道\n",
    "#转置3x3x16x32=====>32x16x3x3\n",
    "weights2[0]=weights2[0].transpose(3,2,0,1)\n",
    "print(\"卷积核转置后的形状：\",weights2[0].shape)             #打印该层的权重的形状：3行3列16通道\n",
    "print(weights2[0])             #打印该层的权重\n",
    "#保存卷积权重到二进制bin文件\n",
    "weights2[0].tofile(\"./weight_bin//c2w.bin\")\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"偏置的类型：\",type(weights2[1]))           #打印该层的权重的类型\n",
    "print(\"偏置原本的形状：\",weights2[1].shape)             #打印该层的权重的形状：3行3列16通道\n",
    "print(weights2[1])             #打印该层的权重\n",
    "weights2[1].tofile(\"./weight_bin/c2b.bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=========================================================================\n",
      "第六层全连接的权重维度： 2\n",
      "=========================================================================\n",
      "矩阵权重的类型： <class 'numpy.ndarray'>\n",
      "矩阵权重的总个数： 200704\n",
      "矩阵权重原来的形状： (1568, 128)\n",
      "矩阵权重的数据类型： float32\n",
      "矩阵权重原来的行数： 1568\n",
      "矩阵权重转置后的形状： (128, 1568)\n",
      "[[-0.01719927 -0.03823454 -0.01932273 ... -0.04921417  0.05377471\n",
      "   0.0252989 ]\n",
      " [-0.04144293 -0.0072552   0.00199412 ...  0.0326843   0.05900571\n",
      "  -0.02609367]\n",
      " [-0.04186489 -0.03022121 -0.03938598 ... -0.05268966  0.05642483\n",
      "   0.0467236 ]\n",
      " ...\n",
      " [ 0.04828049  0.01011903 -0.02746451 ...  0.03128258  0.04817855\n",
      "   0.0346797 ]\n",
      " [-0.02966924 -0.01698747  0.04226634 ...  0.05648492  0.03469853\n",
      "  -0.01776249]\n",
      " [ 0.00390895 -0.03033806  0.04755848 ...  0.04072607 -0.03607173\n",
      "   0.02111315]]\n",
      "=========================================================================\n",
      "偏置的类型： <class 'numpy.ndarray'>\n",
      "偏置形状： (128,)\n",
      "128\n",
      "[-0.07601771 -0.06166385 -0.06526529 -0.01232975 -0.05668816 -0.00570615\n",
      " -0.07438132 -0.06994422 -0.05234959 -0.03183017 -0.09601714 -0.09900951\n",
      " -0.07296367 -0.11196744 -0.11407607 -0.04322184 -0.01594684  0.00234236\n",
      " -0.00509172 -0.0559174  -0.01941404 -0.04869796 -0.06330772 -0.09168445\n",
      " -0.00638195 -0.08302958 -0.03197591  0.00854671 -0.03016883 -0.025873\n",
      " -0.04665476 -0.08542843 -0.09432581 -0.08650593 -0.02547176 -0.0496144\n",
      " -0.05146604 -0.0397908  -0.00294    -0.05809828 -0.10709359 -0.03324924\n",
      " -0.02234198 -0.10016985  0.01373601 -0.05794888 -0.02474223 -0.07925839\n",
      " -0.03635804 -0.0396944  -0.02667447 -0.04169565 -0.06943145 -0.02477394\n",
      " -0.09328254 -0.04698148 -0.074591   -0.04016936 -0.06737196 -0.06272913\n",
      " -0.098612   -0.07966571 -0.03038786 -0.03826346 -0.06206752 -0.08524787\n",
      " -0.01501872 -0.03008646 -0.01603508 -0.09894126  0.00484568 -0.05130474\n",
      " -0.11649687 -0.03363297 -0.05504392 -0.0480268  -0.05032796 -0.05169186\n",
      " -0.07878108 -0.07859845 -0.06190477 -0.02281603 -0.03979008 -0.08060233\n",
      " -0.07443091 -0.11031758 -0.07412477 -0.06336088 -0.05591452 -0.03547946\n",
      " -0.08668626 -0.07401926 -0.06125722 -0.07418317 -0.05678617 -0.03464184\n",
      " -0.06553888 -0.05835684 -0.09319167 -0.08607581  0.00132617 -0.07003727\n",
      " -0.06620283 -0.07956946 -0.05701146  0.00026674 -0.03764935 -0.06709911\n",
      " -0.01725797 -0.06309431 -0.04590928 -0.04002813 -0.06400787 -0.10044698\n",
      " -0.06665077 -0.11387622 -0.08875483 -0.11126214 -0.05121384 -0.01532098\n",
      " -0.07129136 -0.07491609 -0.05565032 -0.04835516 -0.05430748 -0.02287147\n",
      " -0.03523553 -0.03320223]\n"
     ]
    }
   ],
   "source": [
    "#获取第六层全连接的权重和偏置\n",
    "layer6= model.get_layer(index=6) #获取改层\n",
    "weights6 = layer6.get_weights()   #获取该层的参数W和b\n",
    "print('=========================================================================')\n",
    "print(\"第六层全连接的权重维度：\",len(weights6))              #该层权重的的维度：权重和偏置两项\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"矩阵权重的类型：\",type(weights6[0]))            #打印该层的权重的类型\n",
    "print(\"矩阵权重的总个数：\",weights6[0].size)           #权重矩阵的总元素个数\n",
    "print(\"矩阵权重原来的形状：\",weights6[0].shape)          #权重矩阵的形状：\n",
    "\n",
    "print(\"矩阵权重的数据类型：\",weights6[0].dtype)          #权重矩阵的形状：\n",
    "print(\"矩阵权重原来的行数：\",weights6[0].shape[0])          #权重矩阵的形状：\n",
    "weights6[0]=weights6[0].transpose(1,0)\n",
    "print(\"矩阵权重转置后的形状：\",weights6[0].shape)          #权重矩阵的形状\n",
    "print(weights6[0])             #打印该层的权重\n",
    "#保存卷积权重到二进制bin文件\n",
    "\n",
    "weights6[0].tofile(\"./weight_bin//f6w_t.bin\")\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"偏置的类型：\",type(weights6[1]))           #打印该层的偏置的类型\n",
    "print(\"偏置形状：\",weights6[1].shape)             #打印该层的偏置的形状：128x1\n",
    "\n",
    "print(len(weights6[1]))                          #权重矩阵的列数=输入的全连接层输出的元素个数=128\n",
    "print(weights6[1])                               #打印该层的偏置\n",
    "weights6[1].tofile(\"./weight_bin/f6b.bin\")\n",
    "#weights6[1].tofile('.//fc2_weight.txt', sep=' ',format='%f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=========================================================================\n",
      "2\n",
      "=========================================================================\n",
      "矩阵权重的类型： <class 'numpy.ndarray'>\n",
      "矩阵权重的总个数： 1280\n",
      "矩阵权重的形状： (128, 10)\n",
      "矩阵权重转置后的形状： (10, 128)\n",
      "[[ 6.5374054e-02  8.0505675e-03  1.8855231e-02 ...  1.1503628e-01\n",
      "   1.7478634e-01 -3.4651875e-01]\n",
      " [-2.1446282e-01  8.0545016e-02  1.0711244e-01 ... -2.1804452e-02\n",
      "  -2.1660490e-01 -3.3106476e-01]\n",
      " [ 7.2557196e-02  1.1911153e-01 -2.4351113e-01 ... -2.2517715e-02\n",
      "   1.4211325e-01 -2.5358477e-01]\n",
      " ...\n",
      " [ 4.9617540e-02  1.3024026e-01  9.0554237e-02 ... -1.7421724e-02\n",
      "  -2.4326348e-01  1.2257293e-01]\n",
      " [-7.0328094e-02  6.0543753e-02 -3.3804965e-01 ...  1.5849833e-01\n",
      "  -1.7901991e-01  2.8468052e-02]\n",
      " [-1.5213530e-01 -7.1544036e-02 -7.9816600e-05 ...  1.8914089e-01\n",
      "   1.0847200e-01  6.6742025e-02]]\n",
      "=========================================================================\n",
      "偏置的类型： <class 'numpy.ndarray'>\n",
      "偏置形状： (10,)\n",
      "10\n",
      "[-0.01243159  0.09214074 -0.06272949 -0.02321226 -0.08162592 -0.10588994\n",
      " -0.0844419  -0.11360486  0.13890822  0.1077631 ]\n"
     ]
    }
   ],
   "source": [
    "#获取第八层全连接的权重和偏置\n",
    "layer8= model.get_layer(index=8) #获取改层\n",
    "weights8= layer8.get_weights()   #获取该层的参数W和b\n",
    "print('=========================================================================')\n",
    "print(len(weights8))              #该层权重的的维度：权重和偏置两项\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"矩阵权重的类型：\",type(weights8[0]))            #打印该层的权重的类型\n",
    "print(\"矩阵权重的总个数：\",weights8[0].size)           #权重矩阵的总元素个数 1280\n",
    "print(\"矩阵权重的形状：\",weights8[0].shape)          #权重矩阵的形状：128x10\n",
    "\n",
    "weights8[0]=weights8[0].transpose(1,0)\n",
    "print(\"矩阵权重转置后的形状：\",weights8[0].shape)          #权重矩阵的形状：行3列16通道\n",
    "\n",
    "print(weights8[0])             #打印该层的权重\n",
    "#保存卷积权重到二进制bin文件\n",
    "weights8[0].tofile(\"./weight_bin//f8w_t.bin\")\n",
    "\n",
    "print('=========================================================================')\n",
    "print(\"偏置的类型：\",type(weights8[1]))           #打印该层的偏置的类型\n",
    "print(\"偏置形状：\",weights8[1].shape)             #打印该层的偏置的形状：10x1\n",
    "print(len(weights8[1]))                          #权重矩阵的列数=输入的全连接层输出的元素个数=128\n",
    "print(weights8[1])                               #打印该层的偏置\n",
    "weights8[1].tofile(\"./weight_bin1/f8b.bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
